import { BrowserWindow, app } from 'electron';
import path from 'path';
import { resolveHtmlPath } from '../util';

function createAIWindow(args: any) {
  const RESOURCES_PATH = app.isPackaged
    ? path.join(process.resourcesPath, 'assets')
    : path.join(__dirname, '../../assets');

  const getAssetPath = (...paths: string[]): string => {
    return path.join(RESOURCES_PATH, ...paths);
  };

  const preloadPath = app.isPackaged
    ? path.join(__dirname, 'preload.js')
    : path.join(__dirname, '../../.erb/dll/preload.js');
  const newWindow = new BrowserWindow({
    width: args.width,
    height: args.height,
    icon: getAssetPath('icon.png'),
    webPreferences: {
      preload: preloadPath,
      webSecurity: false, // 允许访问本地文件
    },
  });
  newWindow.loadURL(resolveHtmlPath('ai.html'));

  newWindow.on('closed', () => {
    // 窗口关闭时释放引用
  });
  return newWindow;
}

export default createAIWindow;
